package com.gzx.risk.core.utils;

import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ParseVariableUtil {


    /**
     * 解析V2标签变量
     * @param jsonResult
     * @return Map
     */
    public static Map<String,Object> parseVariable(JSONObject jsonResult)
    {
        Map<String,Object> mapResult=new HashMap<>();
        mapResult.putAll(buildScoreData(jsonResult.getString("score1"),0));
        mapResult.putAll(buildScoreData(jsonResult.getString("score2"),1));
        mapResult.putAll(buildScoreData(jsonResult.getString("score3"),2));
        mapResult.putAll(buildScoreData(jsonResult.getString("score4"),3));
        mapResult.putAll(buildScoreData(jsonResult.getString("score5"),4));
        mapResult.putAll(buildScoreData(jsonResult.getString("score6"),5));
        mapResult.putAll(buildScoreDatOther(jsonResult.getString("score7"),6));
        mapResult.putAll(buildScoreDatOther(jsonResult.getString("score8"),7));
        return mapResult;
    }

    private static Integer ConvertToInt(String str)
    {
        return Integer.parseInt(str);

    }

    private static Map<String,Object> buildScoreData(String score, Integer idx)
    {
        Map<String,Object> mapResult=new HashMap<>();

        mapResult.put(String.format("v%s", 5*idx+1),ConvertToInt(score.substring(1,3)));
        mapResult.put(String.format("v%s", 5*idx+2),ConvertToInt(score.substring(4,5)));
        mapResult.put(String.format("v%s", 5*idx+3),ConvertToInt(score.substring(5,6)));
        mapResult.put(String.format("v%s", 5*idx+4),ConvertToInt(score.substring(6,7)));
        mapResult.put(String.format("v%s", 5*idx+5),ConvertToInt(score.substring(7,8)));
        return mapResult;
    }

    private static Map<String,Object> buildScoreDatOther(String score, Integer idx)
    {
        Map<String,Object> mapResult=new HashMap<>();

        Integer startIdx=5*idx;

        if(idx.equals(7)){
            startIdx=33;
        }
        mapResult.put(String.format("v%s", startIdx+1),ConvertToInt(score.substring(1,3)));
        mapResult.put(String.format("v%s", startIdx+2),ConvertToInt(score.substring(4,6)));
        mapResult.put(String.format("v%s", startIdx+3),ConvertToInt(score.substring(6,8)));
        return mapResult;
    }


    /**
     *  数据解析示例
     */
    public static void main(String[] args){

        String data="{\"code\":\"0000\",\"msg\":\"查询成功\",\"token\":\"15ecd8b44a25484db289f28e3d04fa2d\",\"data\":{\"score6\":\"405.0001\",\"score7\":\"434.3550\",\"score8\":\"534.9400\",\"score2\":\"414.0005\",\"score3\":\"513.0001\",\"score4\":\"813.0004\",\"score5\":\"708.0003\",\"score1\":\"527.0006\"}}";

        data="{\"code\":\"0000\",\"msg\":\"查询成功\",\"token\":\"871fa5739f104647abeac6d406ad8398\",\"data\":{\"score6\":\"401.0001\",\"score7\":\"435.3500\",\"score8\":\"735.0000\",\"score2\":\"501.0001\",\"score3\":\"301.0001\",\"score4\":\"602.0002\",\"score5\":\"501.0001\",\"score1\":\"702.0002\"}}";

        data="{\"code\":\"0000\",\"msg\":\"查询成功\",\"token\":\"6a1a8e61f7444eee9cd6f5cf5fc4ce6b\",\"data\":{\"score6\":\"600.0000\",\"score7\":\"384.8400\",\"score8\":\"500.0000\",\"score2\":\"304.0000\",\"score3\":\"700.0000\",\"score4\":\"802.0000\",\"score5\":\"302.0000\",\"score1\":\"604.0000\"}}";

        data="{\"code\":\"0000\",\"msg\":\"查询成功\",\"token\":\"88d1b6b1aecd485cb16c0a99adabca7c\",\"data\":{\"score6\":\"400.0000\",\"score7\":\"405.0500\",\"score8\":\"700.0000\",\"score2\":\"306.0566\",\"score3\":\"700.0000\",\"score4\":\"502.0222\",\"score5\":\"302.0222\",\"score1\":\"706.0566\"}}";

//        data="{\"code\":\"0000\",\"msg\":\"查询成功\",\"token\":\"c1db49eb38bc4ddfac76a96d977847f0\",\"data\":{\"score6\":\"603.0003\",\"score7\":\"627.2700\",\"score8\":\"335.0031\",\"score2\":\"519.0009\",\"score3\":\"704.0004\",\"score4\":\"409.0009\",\"score5\":\"306.0006\",\"score1\":\"723.0009\"}}";

        JSONObject jsonData=JSONObject.parseObject(data);

        System.out.println("===========TanZhi Api Response Data=============");

        System.out.println(jsonData);

        Map<String,Object> parseResult=parseVariable(jsonData.getJSONObject("data"));

        System.out.println("===========parse risk V2 variable result=============");

        System.out.println(JSONObject.toJSONString(parseResult));
    }
}
